<?php
class Admin_UserController extends Zend_Controller_Action 
{
	
	/**
	 * Initialize the action
	 *
	 * @return void
	 */
	public function init() 
	{
		$this->view->user_roles = new Model_UserRole();
	}
	
	/**
	 * The default action
	 *
	 * Render the user management interface
	 *
	 * @return void
	 */
	public function indexAction() 
	{
		$u = new Model_User();
		
		$this->view->users = $u->getAllUsers($this->_request->isPost());
	}
	
	
	/**
     * Add action
     *
     * Add a new user
     *
     * @return void
     */
    public function createAction()
    {
        if ($this->_request->isPost ()) {
        	$username 	= 	Bcms_Filter_Post::get('addname');
            $password 	= 	Bcms_Filter_Post::get('addpassword');
            $email 		= 	Bcms_Filter_Post::get('addemail');
            $role 		= 	Bcms_Filter_Post::get('addrole');
            
            $password = md5($password);
            
            $params = array(
            	'username'	=>	"$username",
            	'password'	=>	"$password",
            	'email'		=>	"$email",
            	'role'		=>	"$role"
            );
            
        	$u = new Model_User($params);
        	$id = $u->createUser($params);
            if($id) {
                $this->_redirect('/admin/user/' );
            }
        }
    }
    
    
	/**
	 * Edit action
	 *
	 * Edit an existing user
	 *
	 * @return void
	 */
	public function editAction() 
	{
		$id = $this->_request->getParam('id', 0);
		$u = new Model_User();
		$this->view->user = $u->find($id)->current();
	}
	
	
	
	/**
     * Update my account action
     *
     * @return void
     */
    public function updateMyUserAction()
    {
        $u = new Model_User();
        if ($this->_request->isPost()) {
            $id = Bcms_Filter_Post::int('id');
            
            $p_modified = Bcms_Filter_Post::int('p_modified');
            if($p_modified){
            	$password = Bcms_Filter_Post::get('password');
            	$passwordConfirm = Bcms_Filter_Post::get('confirmation');
            }
            
            $email = Bcms_Filter_Post::get('email');
            $addrole = Bcms_Filter_Post::get('role');
            
            $u->updatePassword($id,$p_modified, $password, true, $passwordConfirm,$email, $addrole);
        }

    /*    $user = $u->updateFromPost();
        $id = $user->id;*/

       $this->_redirect('/admin/user/' );
    }
    
    /**
     * Delte my account action
     * @return void
     */
    public function deleteUserAction()
    {
    	$u = new Model_User();
    	$delete= Bcms_Filter_Post::raw('delete');
    	foreach($delete as $id => $v){
    		$u->delete('id = ' . $id);
    	}
    	$this->_redirect('/admin/user/' );
    }
    
    /**
     * Ajax check to the username
     * @param:1 Can insert operation;0 No insert operation
     * @return int
     */
    public function ajaxUsernameAction()
    {
    	$addname = $this->_request->getParam('addname', 0);
    	$u = new Model_User();
    	$suc = $u->getUserByName($addname);
    	if($suc){
    		echo '0';
    	}else{
    		echo '1';
    	}
    	exit();
    }
    
     //==User Role==========================================================
    
	
	public function indexRoleAction()
	{
        $this->view->roles = new Model_UserRole();
	}
	
	/*
	 *Permit action
	 *
	 * Permit list || Update permission 
	 */
	
	public function permissionRoleAction() 
	{
		if($this->_request->isPost()){
			$p_id = Bcms_Filter_Post::int('id');
			$p_acl_resources  = Bcms_Filter_Post::raw('acl_resources');
			$ur = new Model_UserRole();
			$rows = $ur->updateAclResources($p_id,$p_acl_resources );
			$this->_redirect('/admin/user-role' );
		}else if($this->_request->isGet()){
			$id = $this->_request->getParam('id', 0);
			 if ($id > 0) {
	            $ur = new Model_UserRole();
	            $row = $ur->find($id)->current();
	            //$this->view->user = $row;
	            $this->view->id = $id;
	            $this->view->userPermissions = unserialize($row->u_acl_resources);
	        }
		}
	}
	
	/**
     * Create action
     *
     * @return void
     */
    public function createRoleAction()
    {
        if ($this->_request->isPost()) {
            // NOTE: we will turn this into a Zend_Form after were sure it will work this way
            $ur = new Model_UserRole();
            $name = Bcms_Filter_Post::get('addname');
            $detail = Bcms_Filter_Post::get('detail');
            
            $id = $ur->createRole($name, $detail);
            $this->_redirect('/admin/user-role' );
            return ;
        }
    }
    
	/**
     * Ajax check to the role
     * @param:1 Can insert operation;0 No insert operation
     * @return int
     */
    public function ajaxRoleAction()
    {
    	$addname = $this->_request->getParam('addname', 0);
    	$u = new Model_UserRole();
    	$suc = $u->getUserByName($addname);
    	if($suc){
    		echo '0';
    	}else{
    		echo '1';
    	}
    	exit();
    }
    
     /**
     * Edit action
     *
     * Edit a menu
     *
     * @return void
     */
    public function editRoleAction()
    {
        if ($this->_request->isGet()) {
            $id = $this->_request->getParam('id', 0);
            $detail = $this->_request->getParam('detail', 0);
            
            $ur = new Model_UserRole();
            $ur->updateRole($id, $detail);
//            echo $detail,',',$id;exit();
            
           $this->_redirect('/admin/user-role' );
        }
    }
    
 	/**
     * Delte role action
     * @return void
     */
    public function deleteRoleAction()
    {
    	
    	$u = new Model_UserRole();
    	$delete= Bcms_Filter_Post::raw('delete');
    	foreach($delete as $id => $v){
    		$u->delete('id = ' . $id);
    	}
    	
    	$this->_redirect('/admin/user-role' );
    }
	
	

}